<?php
namespace app\mobile\controller;

use app\model\Theme; 
use app\model\Worker; 
use app\model\Order; 

class WRank extends Base
{
    public function W_rank()
    {
global $_G, $_P;
$fans = $this->checkuser();
$themeid = intval($_P['themeid']);
if($themeid < 0){
	return_msg('管理后台地址不正确');
}
$system=$_G['system'];
$theme = Theme::getThemeById($themeid);
$config = 	Theme::getConfig($themeid);
if(empty($theme['id'])){
	return_msg('管理后台地址不正确');
}
$myworker = Worker::getThemeWorkerByOpenidNew($_G['openid'],$themeid);
if(!($system['ruanjian']!=3 || $config['showshop']==0 || ($system['ruanjian']==3 && $config['showshop']==$myworker['company']))){
	$myworker = "";
}
if (empty($myworker)) {
	return_msg('您不是员工,无权访问');
}
$msg = Worker::gethoutaistats();
if (!empty($msg)) {
	return_msg($msg);
}

$do = 'w_rank';

$op = $_P['op'];

if ($myworker['level'] == '1') {
	$company = empty($_P['company'])?$myworker['company']:$_P['company'];
	$shop=db_find("SELECT * FROM ".tablename(t_shangjia)." WHERE id= '{$company}'");
	$shopname=$shop['name'];
}

if (!empty($myworker['company'])) {
	$company = " and w.company = " . $myworker['company'];
}else{
	$company = "";
}


$paixu = $_P['paixu'];
$show = $_P['show'];
if ($show =='tuiguang') {
	if ($paixu == 'zf') {
		$paixutxt = "order by forward desc,record desc";
	} elseif ($paixu == 'll') {
		$paixutxt = "order by scansum desc,record desc";
	}else{
		$paixu='';
		$paixutxt = "order by newfans desc,record desc";
	}
}else{
	if ($paixu == 'fee') {
		$paixutxt = "order by fee desc,record desc";
	} elseif ($paixu == 'xx') {
		$paixutxt = "order by recordxx desc,record desc";
	} else {
		$paixu='';
		$paixutxt = "order by record desc,recordxx desc";
	}
}


if ($op == 'search') {
	$starttime = strtotime($_P['starttime']);
	$endtime = empty($_P['endtime']) ? strtotime(date('Y-m-d 23:59:59',TIMESTAMP)): strtotime($_P['endtime'])+59 ;
	//商家排行
	if ($config['showalldata']==0) {
		$noalldata = " and r.themeid =" .$themeid;
	}
    if ($system['ruanjian']==3 && $config['showshop']==0) {
		$companylist = db_select("SELECT s.name as name,sum(case when r.status!=3 THEN 1 else 0 END) as record,sum(case when r.status=3 THEN 1 else 0 END) as recordxx,sum(case when r.status!=3 THEN r.fee else 0 END) as fee FROM " . tablename ( t_record ) . " r
		left join " . tablename ( t_worker ) . " w on w.uid = r.workerid and w.themeid = r.themeid 
		left join " . tablename ( t_shangjia ) . " s on w.company = s.id
		WHERE 1=1 " . $noalldata . " and r.status > 0 and r.addtime > '{$starttime}' and r.addtime <= '{$endtime}' group by w.company " . $paixutxt . ",w.company" );
	}
	//自家团队排行

	$teamlist = db_select("SELECT t.name as name,sum(case when r.status!=3 THEN 1 else 0 END) as record,sum(case when r.status=3 THEN 1 else 0 END) as recordxx,sum(case when r.status!=3 THEN r.fee else 0 END) as fee FROM " . tablename ( t_record ) . " r

	left join " . tablename ( t_worker ) . " w on w.uid = r.workerid and w.themeid = r.themeid 
	left join " . tablename ( t_team ) . " t on w.team = t.id
	WHERE 1=1 " . $noalldata . " and r.status > 0 and r.addtime > '{$starttime}' and r.addtime <= '{$endtime}' " . $company. "  group by w.team " . $paixutxt . ",w.team" );

	//个人团队排行
	$workerlist = db_select("SELECT w.username as name,sum(case when r.status!=3 THEN 1 else 0 END) as record,sum(case when r.status=3 THEN 1 else 0 END) as recordxx,sum(case when r.status!=3 THEN r.fee else 0 END) as fee FROM " . tablename ( t_record ) . " r

	left join " . tablename ( t_worker ) . " w on w.uid = r.workerid and w.themeid = r.themeid 
	WHERE 1=1 " . $noalldata . " and r.status > 0 and r.addtime > '{$starttime}' and r.addtime <= '{$endtime}' " . $company. "  group by w.uid " . $paixutxt . ",w.team" );


	//总数

	$zongshu = db_find("SELECT sum(case when r.status!=3 THEN 1 else 0 END) as record,sum(case when r.status=3 THEN 1 else 0 END) as recordxx,sum(case when r.status!=3 THEN r.fee else 0 END) as fee FROM " . tablename ( t_record ) . " r
	WHERE 1=1 " . $noalldata . " and r.status > 0 and r.addtime > '{$starttime}' and r.addtime <= '{$endtime}' " );
	
	$starttime = date('Y-m-d H:i',$starttime);
	$endtime   = date('Y-m-d H:i',$endtime);
}else{
	$starttime = date('Y-m-d 00:00');
	$endtime   = date('Y-m-d 23:59');
	// //数据统计
	// 	$recordcount = intval(db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_record ) . "
	// 			WHERE  themeid = '{$themeid}' and status > 0  and status!=3" ));
	// 	$recordxxcount = intval(db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_record ) . " as r
	// 		WHERE r.themeid ='{$themeid}'  and r.status = '3' " ));
	// 	$fanscount = intval(db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_fans ) . "
	// 			WHERE themeid = '{$themeid}' " ));
	// 	$ordercount = floatval(db_fetchcolumn ( 'SELECT sum(realfee) FROM ' . tablename ( t_order ) . "
	// 			WHERE themeid = '{$themeid}' and paystate = 1 and realfee > 0 " ));
	// 	$ordercount = sprintf("%.2f", $ordercount);

	// 	$commcount = floatval(db_fetchcolumn ( 'SELECT sum(fee) FROM ' . tablename ( t_comm ) . "
 //            WHERE themeid ='{$themeid}'  and status  = 1 " ));
	// 	$commcount = -sprintf("%.2f", $commcount);

	// //资料统计
 //    $options = "";
 //    if (!empty($config['gudingoptions'])) {
 //        foreach ($config['gudingoptions'] as $key => $value) {
 //            $tempa = intval(db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_record ) . "
 //                WHERE themeid ='{$themeid}' and  `option` like '%" . $value . "%' and status > 0 " ));

 //             $options .="<tr><td>" . $value . "</td><td>" . $tempa . "</td></tr>";
 //        }
 //    }

    // if (!empty($config['shops'])) {
    //     foreach ($config['options'] as $key => $value) {
    //         $tempa = intval(db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_record ) . "
    //             WHERE themeid ='{$themeid}' and  `option` like '%" . $value . "%' and status > 0 " ));

    //          $options .="<tr><td>" . $value . "</td><td>" . $tempa . "</td></tr>";
    //     }
    // }


//商家排行
    if ($system['ruanjian']==3 && ($config['showshop']==0 || $myworker['level'] == '1')) {
    	if ($config['showalldata']==0) {
			$noalldata = " and w.themeid =" .$themeid;
		}
	    // $paixutxt = "order by record desc,s.displayorder,s.id";
	 //    $companylist = db_select("SELECT w.company,s.name as name, sum(r.newfans) as newfans,sum(r.record) as record,sum(r.recordxx) as recordxx,sum(r.recordzx) as recordzx,sum(r.fee) as fee,sum(r.scansum) as scansum,sum(r.forward) as forward, sum(r.refundfee) as refundfee,sum(r.refund) as refund FROM " . tablename ( t_worker ) . " w
		// left join " . tablename ( t_shangjia ) . " s on w.company = s.id
		// left join " . tablename ( t_workerreport ) . " r on w.uid = r.workerid and w.themeid = r.themeid
		// WHERE 1=1 " . $noalldata . " group by w.company " . $paixutxt . ",w.company");

		$sql1 = "SELECT w.company,s.name as name, sum(r.newfans) as newfans,sum(r.record) as record,sum(r.recordxx) as recordxx,sum(r.recordzx) as recordzx,sum(r.fee) as fee,sum(r.scansum) as scansum,sum(r.forward) as forward, sum(r.refundfee) as refundfee,sum(r.refund) as refund FROM " . tablename ( t_worker ) . " w
			left join " . tablename ( t_shangjia ) . " s on w.company = s.id
			left join " . tablename ( t_workerreport ) . " r on w.uid = r.workerid and w.themeid = r.themeid
			WHERE 1=1 " . $noalldata ;
		$sql2 = " group by w.company " . $paixutxt;

		$companylist = db_select($sql1.$sql2);
		$companytoday = db_select($sql1 . " and r.riqi='".date("Y-m-d")."' ". $sql2);
	}
//自家团队排行
	// $paixutxt = "order by record desc,recordxx desc";
	// $paixuteam = $paixutxt .",t.id";
	// $team = db_select("SELECT t.name , w.team, sum(r.newfans) as newfans,sum(r.record) as record,sum(r.recordxx) as recordxx,sum(r.recordzx) as recordzx,sum(r.fee) as fee,sum(r.scansum) as scansum,sum(r.forward) as forward, sum(r.refundfee) as refundfee,sum(r.refund) as refund FROM " . tablename ( t_worker ) . " w
	// 	left join " . tablename ( t_team ) . " t on w.team = t.id
	// 	left join " . tablename ( t_workerreport ) . " r on w.uid = r.workerid and w.themeid = r.themeid
	// 	WHERE w.themeid ='{$themeid}'  " . $company ." group by w.team " . $paixuteam  );
	// foreach ($team as $index => $item) {
	// 	$list = db_select("SELECT w.username as name,w.uid,w.team, sum(r.newfans) as newfans,sum(r.record) as record,sum(r.recordxx) as recordxx,sum(r.recordzx) as recordzx,sum(r.fee) as fee,sum(r.scansum) as scansum,sum(r.forward) as forward, sum(r.refundfee) as refundfee,sum(r.refund) as refund FROM " . tablename ( t_worker ) . " w
	// 		left join " . tablename ( t_workerreport ) . " r on w.uid = r.workerid and w.themeid = r.themeid
	// 		WHERE w.themeid ='{$themeid}' " . $company ." and w.team = " . $item['team'] ." group by w.uid " . $paixutxt .",w.uid" );
	// 	$team[$index]['list']=$list;
	// }

	$sql1 = "SELECT t.name , w.team, sum(r.newfans) as newfans,sum(r.record) as record,sum(r.recordxx) as recordxx,sum(r.recordzx) as recordzx,sum(r.fee) as fee,sum(r.scansum) as scansum,sum(r.forward) as forward, sum(r.refundfee) as refundfee,sum(r.refund) as refund FROM " . tablename ( t_worker ) . " w
		left join " . tablename ( t_team ) . " t on w.team = t.id
		left join " . tablename ( t_workerreport ) . " r on w.uid = r.workerid and w.themeid = r.themeid
		WHERE 1=1 " . $noalldata . " " . $company ;
	$sql2 = " group by w.team " . $paixutxt . ",w.team";

	$teamlist = db_select($sql1.$sql2);
	$teamtoday = db_select($sql1 . " and r.riqi='".date("Y-m-d")."' ". $sql2);

//个人排行

	// $paixutxt = "order by record desc,recordxx desc,w.uid";
	// $workerlist = db_select("SELECT w.username as name ,w.uid, sum(r.newfans) as newfans,sum(r.record) as record,sum(r.recordxx) as recordxx,sum(r.recordzx) as recordzx,sum(r.fee) as fee,sum(r.scansum) as scansum,sum(r.forward) as forward, sum(r.refundfee) as refundfee,sum(r.refund) as refund FROM " . tablename ( t_worker ) . " w
	// left join " . tablename ( t_workerreport ) . " r on w.uid = r.workerid and w.themeid = r.themeid
	// WHERE w.themeid ='{$themeid}' " . $company ." group by w.uid " . $paixutxt );


	
	// if ($config['showalldata']==0) {
	// 	$noalldata = " and w.themeid =" .$themeid;
	// }
	// $notworker = db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_record ) . "
	// 	WHERE status > 0  and status!=3 and workerid = '' and themeid = '{$themeid}' " );

	$sql1 = "SELECT w.username as name ,w.uid, sum(r.newfans) as newfans,sum(r.record) as record,sum(r.recordxx) as recordxx,sum(r.recordzx) as recordzx,sum(r.fee) as fee,sum(r.scansum) as scansum,sum(r.forward) as forward, sum(r.refundfee) as refundfee,sum(r.refund) as refund FROM " . tablename ( t_worker ) . " w
		left join " . tablename ( t_workerreport ) . " r on w.uid = r.workerid and w.themeid = r.themeid
		WHERE 1=1 " .$noalldata. " " . $company ;
	$sql2 = " group by w.uid " . $paixutxt. ",w.uid";

	$workerlist = db_select($sql1.$sql2);
	$workerlisttoday = db_select($sql1 . " and r.riqi='".date("Y-m-d")."' ". $sql2);
}

$shouxu = 1 - Order::shouxufei();
$shouxu100 = Order::shouxufei()*100;

if($_G['companyname'] =='飞驰软件'){
	$return_clicent = get_defined_vars();
return view('/fcworker/fc_rank' , $return_clicent);
}else{
	$return_clicent = get_defined_vars();
return view('/yxworker/w_tongji' , $return_clicent);
}
}
}